package com.jxl.wechat.handler;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工厂类
 */
@Component
public class WeChatMsgFactory implements InitializingBean {

    @Resource
    private List<WeChatMsgHandler> list;

    private Map<WeChatMsgTypeEnums,WeChatMsgHandler> map = new HashMap<>();


    public WeChatMsgHandler getHandler(String msgType){
        WeChatMsgTypeEnums byMsgType = WeChatMsgTypeEnums.getByMsgType(msgType);
        return map.get(byMsgType);
    }


    /**
     * 初始化进行
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        for (WeChatMsgHandler weChatMsgHandler:list) {
            map.put(weChatMsgHandler.getMsgType(),weChatMsgHandler);
        }
    }
}
